package com.yunc.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.modules.sys.entity.SysRoleMenu;
import com.yunc.modules.sys.mapper.SysRoleMenuMapper;
import com.yunc.modules.sys.service.ISysRoleMenuService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色菜单
 *
 * @author congcong tao
 * @date 2018/11/14 19:52
 */
@Service("sysRoleMenuService")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    private final SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public boolean insertList(String roleId, String menuIds) {
        if (StringUtils.isNotEmpty(menuIds)) {
            String[] menuIdArr = menuIds.split(",");
            if (menuIdArr != null && menuIdArr.length > 0) {
                List<SysRoleMenu> roleMenuList = new ArrayList<>();
                for (String menuId : menuIdArr) {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setRoleId(roleId);
                    roleMenu.setMenuId(menuId);
                    roleMenuList.add(roleMenu);
                }
                boolean flag = this.saveBatch(roleMenuList);
                if (!flag) {
                    return false;
                }
                return true;
            }
        }
        return false;
    }
}
